package org.bt4girls.oauth.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.bt4girls.common.model.oauth.TbPermission;
import org.bt4girls.common.model.result.ErrorCode;
import org.bt4girls.common.model.result.ServiceResult;
import org.bt4girls.oauth.mappy.TbPermissionMapper;
import org.bt4girls.oauth.mappy.TbRoleMapper;
import org.bt4girls.oauth.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private TbPermissionMapper permissionMapper;
    @Autowired
    private TbRoleMapper roleMapper;

    @Override
    public ServiceResult<String> addPermission(String path, String actionId, String name) {
        if (StringUtils.isAllBlank(path, actionId)) {
            ServiceResult.exception(ErrorCode.REQUEST_PARAM_ERROR, null);
        }
        if (permissionMapper.selectByRoutePath(path) != null) {
            ServiceResult.exception(ErrorCode.PERMISSION_IS_EXIST, null);
        }
        if (permissionMapper.selectByActionId(actionId) != null) {
            ServiceResult.exception(ErrorCode.PERMISSION_IS_EXIST, null);
        }
        permissionMapper.insertPermission(path, actionId, name);
        return ServiceResult.success(null);
    }


    @Override
    public ServiceResult<String> addRole(String roleName, String description) {
        //role 不存在
        if (roleMapper.selectByRoleName(roleName) != null) {
            ServiceResult.exception(ErrorCode.ROLE_IS_EXIST, null);
        }
        //

        return null;
    }
}
